package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.LoginVo;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author stone
 * @date 2023/04/05 15:17
 */
@WebServlet("/admin/user/*")
public class AdminUserServlet extends WdBaseServlet {

    UserService userService = new UserServiceImpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String order = request.getParameter("order");
        String sort = request.getParameter("sort");
        //测试不同情况
        String mobile = request.getParameter("mobile");
        String username = request.getParameter("username");
        //提前搞定参数
        if ("".equals(mobile)) {
            mobile = null;
        }
        //
        System.out.println("---------------------------------测试参数-----------------------------");
        System.out.print("page = " + page);
        System.out.print(" limit = " + limit);
        System.out.print(" order = " + order);
        System.out.print(" sort = " + sort);
        System.out.print(" mobile = " + mobile);
        System.out.print(" username = " + username);
        System.out.println();
        //
        CommonData data = userService.query(page, limit, sort, order);
        if (mobile == null && username == null || ("".equals(mobile) && "".equals(username))) {
            System.out.println("此时全都是是空的");
        }
        if ((mobile == null || "".equals(mobile)) && username != null && ("".equals(username) == false)) {
            System.out.println("根据姓名来模糊查找" + username);
            List<MarketUser> userList = userService.selectByFuzzyName(username);
            System.out.println("userList.size() = " + userList.size());
            for (MarketUser marketUser : userList) {
                System.out.println(marketUser);
            }
            CommonData mohuData = CommonData.data(userList);
            return BaseRespVo.ok(mohuData);
        }
        if (mobile != null && (username == null || "".equals(username))) {
            System.out.println("根据电话号码来查找:" + mobile);
            MarketUser marketUser = userService.selectByMobile(mobile);
            //
            System.out.println(marketUser);
            //
            List<MarketUser> users = new ArrayList<>();
            users.add(marketUser);
            //
            System.out.println("users.size() = " + users.size());
            //
            CommonData mobileData = CommonData.data(users);
            return BaseRespVo.ok(mobileData);
        }
        //
        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        //格式化时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        java.util.Date updateTime = new Date();//获得当前时间
        // String updateTime = df.format(date);//将当前时间转换成特定格式的时间字符串，这样便可以插入到数据库中
        //
        System.out.println("进入更新");
        // Integer id = Integer.parseInt(request.getParameter("id"));
        String jsonStr = request.getReader().readLine();
        MarketUser updateMarketUser = JacksonUtil.read(jsonStr, MarketUser.class);
        System.out.println(updateMarketUser);
        //设置更新时间
        updateMarketUser.setUpdateTime(updateTime);
        //
        int update = userService.update(updateMarketUser);//成功返回1
        return BaseRespVo.ok(updateMarketUser);
    }

    @SneakyThrows
    public BaseRespVo detail(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入用id精确查找");
        Integer id = Integer.parseInt(request.getParameter("id"));
        System.out.println("id = " + id);
        MarketUser marketUser = userService.selectById(id);
        System.out.println(marketUser);
        return BaseRespVo.ok(marketUser);
    }
}
